/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.switz.agendacita.persistence;

import com.switz.agendacita.dto.PersonalHorario;
import com.switz.agendacita.persistence.iface.PersonalHorarioDao;
import com.switz.agendacita.vo.DiasLaborales;

import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author jose angel
 */
@Repository
public class PersonalHorarioDaoImpl extends JdbcDaoSupport implements PersonalHorarioDao {

    @Autowired
    public PersonalHorarioDaoImpl(DataSource dataSource) {

        setDataSource(dataSource);
    }

    @Override
    public PersonalHorario getHoraEntradaSalidaPersonal(String idEmpresa,String idPersonal, int dia) {
        String query = "SELECT entrada,salida FROM personalhorarios where idempresa=? and idpersonal=? and  dia=?";
        List<PersonalHorario> personalDiasList = getJdbcTemplate().query(query, new Object[]{idEmpresa,idPersonal, dia},
                ParameterizedBeanPropertyRowMapper.newInstance(PersonalHorario.class));
        PersonalHorario personalDias = personalDiasList.size() > 0 ? personalDiasList.get(0) : null;

        return personalDias;






    }

    @Override
    public void deleteDiasLaboralesPorIdPersonal(String idEmpresa,String idPersonal) {
        String query = "delete from personalhorarios where idempresa=? and idpersonal=?";
        getJdbcTemplate().update(query, new Object[]{idEmpresa,idPersonal});
    }

    @Override
    public void insertBatchDiasLaboralesPorPersonal(List personalDiasList) {

        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(
                getJdbcTemplate().getDataSource())
                .withTableName("personalhorarios");

        SqlParameterSource[] array =SqlParameterSourceUtils.createBatch(personalDiasList.toArray());

       

        simpleJdbcInsert.executeBatch(array);

    }

	@Override
	public List selectDiasLaborales(String idPersonal) {
		String query = "SELECT personalhorarios.id, personalhorarios.idempresa, " +
				"personalhorarios.idpersonal, personalhorarios.dia, personalhorarios.entrada, " +
				"personalhorarios.salida, personalnohorarios.horainicio, personalnohorarios.horafin " +
				"FROM personalhorarios LEFT OUTER JOIN personalnohorarios on " +
				"personalhorarios.idpersonal=personalnohorarios.idpersonal and " +
				"personalhorarios.dia=personalnohorarios.dia where personalhorarios.idpersonal=? " +
				"ORDER BY personalhorarios.dia";
        
		List diasLaborales = getJdbcTemplate().query(query, new Object[]{idPersonal},
                ParameterizedBeanPropertyRowMapper.newInstance(DiasLaborales.class));
        
        
        return diasLaborales;	
	}
}
